<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\items\Items as ItemsModel;
use app\admin\model\items\Itemscategory;
use app\admin\model\items\Position;
use app\admin\model\items\Position2;

use think\Request;
/**
 * 首页接口
 */
class Items extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    public function getInfo(Request $request){
        $item_bn= $request->get('item_bn');
        $item_bn = str_replace('，', ',', $item_bn);
        $model = new ItemsModel;
        $row = $model->alias('a')->field('a.*,b.name as itemscategory,c.name as positionname,d.name as position2name')
        ->join('itemscategory b','a.itemscategory_id=b.id','left')
        ->join('position c','a.position=c.id','left')
        ->join('position2 d','a.position2=d.id','left')
        ->where('a.item_bn','in',$item_bn)
        ->select(); 
        $this->success('请求成功!',['list'=>$row]);
    }
    public function getCategory(){
        $category = new Itemscategory;
        $row = $category->field('id as value,name as text')->select();
        $this->success('请求成功',['list'=>$row]);
    }
    public function getPosition(){
        $category = new Position;
        $row = $category->field('id as value,name as text')->select();
        $this->success('请求成功',['list'=>$row]);
    }
    public function getPosition2(){
        $category = new Position2;
        $row = $category->field('id as value,name as text')->select();
        $this->success('请求成功',['list'=>$row]);
    }
    public function edit(Request $request){
        $data = $request->post();
        if(empty($data['item_bn']) || !is_numeric($data['item_bn'])){
            $this->error('请输入商品编码!');
        }
        if(empty($data['name'])){
            $this->error('请输入商品名称!');
        }
        if(empty($data['position'])){
            $this->error('请输入商品位置!');
        }
        $model = new ItemsModel;
        $arr = [
            'item_bn'=>$data['item_bn'],
            'name'=>$data['name'],
            'position'=>$data['position'],
            'position2'=>$data['position2'],
            'position3'=>$data['position3'],
            'itemscategory_id'=>$data['itemscategory_id'],

        ];
        $row = $model->where('item_bn',$data['item_bn'])->find();
        if(!$row){
            $result =  $model->save($arr);
        }else{
            $result = $model->where('item_bn',$data['item_bn'])->update($arr);
        }
        $this->success($result);
    }
}
